package com.linxz.ui.divider;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.linxz.ui.GuiHelper;

import static android.widget.LinearLayout.VERTICAL;

/**
 * @author Linxz
 * 创建日期：2019年07月09日 12:36
 * version：v4.0.1
 * 描述：
 */
public class DividerGridDecoration extends RecyclerView.ItemDecoration {
    private Drawable mDivider;
    private int mIntrinsicHeight;
    private int mIntrinsicWidth;
    //RecyclerView  item左右两边
    private boolean mPaddingEdgeSide = true;
    //RecyclerView控件顶部分割
    private boolean mPaddingStart = true;
    //RecyclerView控件头部和底部视图
    private boolean mPaddingHeaderFooter = false;
    //最后一行
    private boolean mDrawLastItem = true;
    private int mPaddingLeft;
    private int mPaddingRight;
    private int mPaddingTop;
    private int mPaddingBottom;


    public Drawable getDivider() {
        return mDivider;
    }

    public void setDivider(Drawable divider) {
        mDivider = divider;
    }

    public int getIntrinsicHeight() {
        return mIntrinsicHeight;
    }

    public void setIntrinsicHeight(int intrinsicHeight) {
        mIntrinsicHeight = intrinsicHeight;
    }

    public int getIntrinsicWidth() {
        return mIntrinsicWidth;
    }

    public void setIntrinsicWidth(int intrinsicWidth) {
        mIntrinsicWidth = intrinsicWidth;
    }

    public boolean isDrawLastItem() {
        return mDrawLastItem;
    }

    public void setDrawLastItem(boolean drawLastItem) {
        mDrawLastItem = drawLastItem;
    }

    public int getPaddingLeft() {
        return mPaddingLeft;
    }

    public void setPaddingLeft(int paddingLeft) {
        mPaddingLeft = paddingLeft;
    }

    public int getPaddingRight() {
        return mPaddingRight;
    }

    public void setPaddingRight(int paddingRight) {
        mPaddingRight = paddingRight;
    }

    public int getPaddingTop() {
        return mPaddingTop;
    }

    public void setPaddingTop(int paddingTop) {
        mPaddingTop = paddingTop;
    }

    public int getPaddingBottom() {
        return mPaddingBottom;
    }

    public void setPaddingBottom(int paddingBottom) {
        mPaddingBottom = paddingBottom;
    }

    public DividerGridDecoration(Context context, @DrawableRes int divider) {
        mDivider = ContextCompat.getDrawable(context,divider);
        mIntrinsicHeight=mDivider.getIntrinsicHeight();
        mIntrinsicWidth=mDivider.getIntrinsicWidth();
    }
    public DividerGridDecoration(Drawable divider) {
        mDivider = divider;
        mIntrinsicHeight=mDivider.getIntrinsicHeight();
        mIntrinsicWidth=mDivider.getIntrinsicWidth();
    }

    public DividerGridDecoration(@ColorInt int dividerColor, int intrinsicWidth, int intrinsicHeight) {
        mDivider =new ColorDrawable(dividerColor);
        mIntrinsicHeight=intrinsicWidth;
        mIntrinsicWidth=intrinsicHeight;

    }

    public DividerGridDecoration(Context context,int dividerColor, int intrinsicWidth, int intrinsicHeight) {
        mDivider =new ColorDrawable(ContextCompat.getColor(context,dividerColor));
        mIntrinsicHeight= GuiHelper.getDimensionPixelSize(context,intrinsicWidth);
        mIntrinsicWidth= GuiHelper.getDimensionPixelSize(context,intrinsicHeight);
    }


    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            Rect outRect=measureItemOffset(parent,child);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            int left=child.getLeft()-params.leftMargin-outRect.left;
            int top=child.getTop()-params.topMargin-outRect.top;
            int right=child.getRight()+params.rightMargin+outRect.right;
            int bottom=child.getBottom()+params.bottomMargin+outRect.bottom;
            if(outRect.left>0){
                mDivider.setBounds(left, top+mPaddingTop,left+outRect.left, bottom-mPaddingBottom);
                mDivider.draw(c);
            }
            if(outRect.top>0){
                mDivider.setBounds(left+mPaddingLeft, top,right-mPaddingRight,top+outRect.top);
                mDivider.draw(c);
            }
            if(outRect.right>0){
                mDivider.setBounds(right-outRect.right, top+mPaddingTop,right, bottom-mPaddingBottom);
                mDivider.draw(c);
            }

            if(outRect.bottom>0){
                mDivider.setBounds(left+mPaddingLeft,bottom-outRect.bottom,right-mPaddingRight,bottom);
                mDivider.draw(c);
            }
        }
    }



    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
       /* int position=parent.getChildAdapterPosition(view);
        int itemCount = parent.getAdapter().getItemCount();
        int spanIndex = 0;
        int spanCount=0;
        int orientation=0;
        int headerCount = 0,footerCount = 0;
        if (parent.getAdapter() instanceof RecyclerArrayAdapter){
            headerCount = ((RecyclerArrayAdapter) parent.getAdapter()).getHeaderCount();
            footerCount = ((RecyclerArrayAdapter) parent.getAdapter()).getFooterCount();
        }
        int dataPosition=position-headerCount;
        int dataCount =itemCount-headerCount-footerCount;
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof StaggeredGridLayoutManager) {
            orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
            spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
            spanIndex = ((StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams()).getSpanIndex();

        } else if (layoutManager instanceof GridLayoutManager) {
            orientation = ((GridLayoutManager) layoutManager).getOrientation();
            spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
            spanIndex = ((GridLayoutManager.LayoutParams) view.getLayoutParams()).getSpanIndex();

        } else if (layoutManager instanceof LinearLayoutManager) {
            orientation = ((LinearLayoutManager) layoutManager).getOrientation();
            spanCount = 1;
            spanIndex = 0;
        }

        // 如果是最后一行，则不需要绘制底部
        if (isLastRow(orientation,spanIndex, spanCount,dataPosition, dataCount)) {
            // 最后一行且为最后一列则不绘制
            if (isLastColum(orientation, spanIndex, spanCount,dataPosition, dataCount)) {
                outRect.set(0, 0, 0, 0);
            } else {
                outRect.set(0, 0, mIntrinsicWidth, 0);
            }
        } else if (isLastColum(orientation,spanIndex, spanCount,dataPosition, dataCount)) {
            // 如果是最后一列，则不需要绘制右边
            outRect.set(0, 0, 0, mIntrinsicHeight);
        } else {
            outRect.set(0, 0,mIntrinsicWidth,mIntrinsicHeight);
        }*/
        Rect rect=measureItemOffset(parent,view);
        outRect.set(rect);
    }
    private  Rect measureItemOffset(RecyclerView parent, View view){
        Rect outRect=new Rect();
        int position = parent.getChildAdapterPosition(view);
        int itemCount = parent.getAdapter().getItemCount();
        //方向
        int orientation = 0;
        //每行item个数
        int spanCount = 0;
        //item在每行的index
        int spanIndex = 0;
        //虚拟头部、尾部控件个数
        int headerCount = 0, footerCount = 0;
//        if (parent.getAdapter() instanceof RecyclerArrayAdapter) {
//            headerCount = ((RecyclerArrayAdapter) parent.getAdapter()).getHeaderCount();
//            footerCount = ((RecyclerArrayAdapter) parent.getAdapter()).getFooterCount();
//        }

        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof StaggeredGridLayoutManager) {
            orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
            spanCount = ((StaggeredGridLayoutManager) layoutManager).getSpanCount();
            spanIndex = ((StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams()).getSpanIndex();
        } else if (layoutManager instanceof GridLayoutManager) {
            orientation = ((GridLayoutManager) layoutManager).getOrientation();
            spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
            spanIndex = ((GridLayoutManager.LayoutParams) view.getLayoutParams()).getSpanIndex();
        } else if (layoutManager instanceof LinearLayoutManager) {
            orientation = ((LinearLayoutManager) layoutManager).getOrientation();
            spanCount = 1;
            spanIndex = 0;
        }

        int dataPosition=position-headerCount;
        int dataCount =itemCount-headerCount-footerCount;
        int left=0;
        int right=0;
        int top=0;
        int bottom=0;
        //整除
        if ((dataPosition >= 0 && dataPosition <dataCount)) {
            if (orientation == VERTICAL) {
                if(mPaddingStart&&isFristRow(dataPosition,spanCount)){
                    top=mIntrinsicHeight;
                }
                if(mPaddingEdgeSide){
                    if(isFristColum(orientation,spanIndex,spanCount)){
                        left=mIntrinsicWidth;
                    }
                    right=mIntrinsicWidth;
                }else {
                    if(!isLastColum(orientation,spanIndex,spanCount,dataPosition,dataCount)){
                        right=mIntrinsicWidth;
                    }
                }
                if(!mDrawLastItem&&isLastRow(orientation,spanIndex,spanCount,dataPosition,dataCount)){
                    bottom=0;
                }else{
                    bottom=mIntrinsicHeight;
                }
            } else {

                if(mPaddingStart&&isFristRow(dataPosition,spanCount)){
                    left=mIntrinsicWidth;
                }
                if(mPaddingEdgeSide){
                    if(isFristColum(orientation,spanIndex,spanCount)){
                        bottom=mIntrinsicHeight;
                    }
                    top=mIntrinsicHeight;
                }else {
                    if(!isLastColum(orientation,spanIndex,spanCount,dataPosition,dataCount)){
                        top=mIntrinsicWidth;
                    }
                }
                if(!mDrawLastItem&&isLastRow(orientation,spanIndex,spanCount,dataPosition,dataCount)){
                    right=0;
                }else{
                    right=mIntrinsicWidth;
                }

            }
        } else if (mPaddingHeaderFooter) {
            //头部和底部控件尺寸配置
            if(orientation== OrientationHelper.VERTICAL){
                if(mPaddingEdgeSide){
                    left=right=mIntrinsicWidth;
                }
                if(mPaddingStart){
                    top=mIntrinsicHeight;
                }
            }else{
                if(mPaddingEdgeSide){
                    top=bottom=mIntrinsicHeight;
                }
                if(mPaddingStart){
                    left=mIntrinsicWidth;
                }
            }

        }
        outRect.set(left,top,right,bottom);
        return outRect;
    }

    private  boolean isFristRow(int position,int spanCount){
        boolean result=false;
        if(position<spanCount){
            result=true;
        }
        return  result;
    }
    private boolean isLastColum(int orientation, int spanIndex, int spanCount,
                                int position, int childCount) {
        boolean result=false;
        if(orientation== OrientationHelper.VERTICAL){
            result=(spanIndex+1)%spanCount==0;
        }else{
            result=spanCount-spanIndex+position>=childCount;
        }
        return result;
    }

    private boolean isFristColum(int orientation, int spanIndex, int spanCount) {
        boolean result=false;
        if(orientation== OrientationHelper.VERTICAL){
            result=spanIndex==0;
        }else{
            result=spanIndex==spanCount-1;
        }
        return result;
    }

    private boolean isLastRow(int orientation, int spanIndex, int spanCount,
                              int position,int childCount) {
        boolean result=false;
        if(orientation== OrientationHelper.VERTICAL){

            result=spanCount-spanIndex+position>=childCount;
        }else{
            result=(spanIndex+1)%spanCount==0;
        }
        return result;
    }

    public boolean isPaddingEdgeSide() {
        return mPaddingEdgeSide;
    }

    public void setPaddingEdgeSide(boolean paddingEdgeSide) {
        mPaddingEdgeSide = paddingEdgeSide;
    }

    public boolean isPaddingStart() {
        return mPaddingStart;
    }

    public void setPaddingStart(boolean paddingStart) {
        mPaddingStart = paddingStart;
    }

    public boolean isPaddingHeaderFooter() {
        return mPaddingHeaderFooter;
    }

    public void setPaddingHeaderFooter(boolean paddingHeaderFooter) {
        mPaddingHeaderFooter = paddingHeaderFooter;
    }
}

